Vue.component('show-detail', {
    template: `<el-dialog title="专家回复" :visible.sync="visibleDialog" width="80vw">
    <el-form :inline="true" :form="form">
        <el-form-item>
            <el-input v-model.trim="form.problem" placeholder="请输入专家回复"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="Add">员工回复</el-button>
        </el-form-item>
    </el-form>
    <el-table :data="tableData" stripe border min-height="50vh" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column align="center" prop="problem" label="回复"></el-table-column>
        <el-table-column align="center" prop="Operation_Datetime" width="180" label="回复时间">
            <template v-slot="{row}">
                {{getYMDHMSDate(row.Operation_Datetime)}}
            </template>
        </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">返回</el-button>
    </span></el-dialog>`,
    props: ['id','visible'],
    data() {
        return {
            visibleDialog: false,
            refreshDialog: false,
            tableData: [],
            form: {
                Expert_Problem_SerialNumber:'',
                problem:'',
            },
            total: 0,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
            if (val) {
                this.form.Expert_Problem_SerialNumber = this.id
                this.getList({
                    Data:'business_expert_problem_detail',
                    Condition:"Expert_Problem_SerialNumber='"+this.id+"'",
                })
            }
        },
    },
    methods: {
        Add(){
            const that = this;
            if (this.form.problem) {
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ Add: { business_expert_problem_detail: [that.form] } })
                    },
                    success: function (res) {
                        if (res.Status === 'success') {
                            that.$message.success("提交成功");
                            that.problem = '';
                            that.getList({
                                Data:'business_expert_problem_detail',
                                Condition:"Expert_Problem_SerialNumber='"+that.id+"'",
                            })
                            that.$emit('success')
                        } else {
                            that.$message.error("提交失败，请稍后再试")
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        that.loading = false
                        requestPretreatment(XMLHttpRequest);//config/config.js 响应预检
                    },
                    dataType: "json"
                })
            }else {
                that.$message({
                    message: '请输入回复后再提交',
                    type: 'warning'
                })
            }
            
        },
        getList(data) {
            const that = this
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data,
                success: function (Result) {
                    console.log(Result);
                    that.tableData = Result;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    }
})

new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_expert_problem",
                PaginationNumber: 1,//页数
                RowAmount: 20,//每页数据条数
                Condition: '',
                ExtensionCondition: '',
                Category_SerialNumber:'',
                Expert_SerialNumber:'',
            },
            total: 0,
            dialogVisible:false,
            Expert_Poblem_SerialNumber:'',
            tableData: [],	//表格数据
            ExportData: [],//导出
            defaultProps: {
                children: 'Children',
                label: 'label'
            },
            expert:[],
            category:[],
            loading: false,
            dialogVisible: null,
            isTree:true,
        }
    },
    mounted() {
        this.getData();
        this.getList(this.form);//前端分页
    },
    methods: {
        Add(){
            window.location.href = './add.html'
        },
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        show(row){
            this.dialogVisible = true;
            console.log(row);
            this.Expert_Poblem_SerialNumber = row.Expert_Poblem_SerialNumber;
        },
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_expert',
                    Condition: "Row_Status <> '1'",
                },
                success: function (res) {
                    that.expert = res;
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_category',
                    Condition: "Row_Status <> '1'",
                },
                success: function (res) {
                    that.category = res;
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            let sql = "where 1=1\r\n";
            if (form.Category_SerialNumber) {
                sql = sql + "and business_expert_problem.Category_SerialNumber='"+form.Category_SerialNumber+"'\r\n";
            }
            if (form.Expert_SerialNumber) {
                sql = sql + "and business_expert_problem.Expert_SerialNumber='"+form.Expert_SerialNumber+"'\r\n";
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Operate(command, row) {
            if (command == 'Show') {
                this.Sales_Order = row
                this.dialogVisible = true;
            }
        },
        handleNode(row, node) {
            this.form.Activity_SerialNumber = row.Activity_SerialNumber;
            this.form.Supplier_SerialNumber = row.Supplier_SerialNumber;
            this.getList(this.form);
        },
        Export(form) {
            const that = this;
            this.loading = true;
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            if (data.Sales_Date) {
                sql = sql + "AND business_sales_order.Sales_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            }
            if (data.Activity_SerialNumber) {
                sql = sql + "AND business_member_points_log.Activity_SerialNumber = '" + data.Activity_SerialNumber + "'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    if (Result.length > 0) {
                        that.ExportData = Result;
                        that.$nextTick(() => {
                            let wb = XLSX.utils.table_to_book(document.querySelector("#ExportData"));
                            var wbout = XLSX.write(wb, {
                                bookType: "xlsx",
                                bookSST: true,
                                type: "array"
                            });
                            try {
                                saveAs(
                                    new Blob([wbout], {
                                        type: "application/octet-stream"
                                    }),
                                    "销售订单表" + (+new Date()) + ".xlsx"
                                );
                            } catch (e) {
                                if (typeof console !== "undefined") console.log(e, wbout);
                            }
                            that.$message({
                                message: '已成功导出',
                                type: 'success'
                            })
                            return wbout;
                        })
                    } else {
                        that.$message({
                            message: '暂无可导出的数据',
                            type: 'warning'
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    },
})